interface LanguageInfo {
  code: string
  zhName: string
  enName: string
  pinyin: string
  sort: number
}

export const languages: Record<string, LanguageInfo> = {
  zh: {
    code: 'zh-CN',
    zhName: "中文(简体)",
    enName: "Chinese",
    pinyin: "zhongwenjianti",
    sort: 1
  },
  en: {
    code: 'en-US',
    zhName: "英语",
    enName: "English",
    pinyin: "yingyu",
    sort: 2
  },
  jp: {
    code: 'ja-JP',
    zhName: "日语",
    enName: "Japanese",
    pinyin: "riyu",
    sort: 3
  },
  th: {
    code: 'th-TH',
    zhName: "泰语",
    enName: "Thai",
    pinyin: "taiyu",
    sort: 4
  },
  spa: {
    code: 'es-ES',
    zhName: "西班牙语",
    enName: "Spanish",
    pinyin: "xibanyayu",
    sort: 5
  },
  ara: {
    code: 'ar-SA',
    zhName: "阿拉伯语",
    enName: "Arabic",
    pinyin: "alaboyu",
    sort: 6
  },
  fra: {
    code: 'fr-FR',
    zhName: "法语",
    enName: "French",
    pinyin: "fayu",
    sort: 7
  },
  kor: {
    code: 'ko-KR',
    zhName: "韩语",
    enName: "Korean",
    pinyin: "hanyu",
    sort: 8
  },
  ru: {
    code: 'ru-RU',
    zhName: "俄语",
    enName: "Russian",
    pinyin: "eyu",
    sort: 9
  },
  de: {
    code: 'de-DE',
    zhName: "德语",
    enName: "German",
    pinyin: "deyu",
    sort: 10
  },
  pt: {
    code: 'pt-PT',
    zhName: "葡萄牙语",
    enName: "Portuguese",
    pinyin: "putaoyayu",
    sort: 11
  },
  it: {
    code: 'it-IT',
    zhName: "意大利语",
    enName: "Italian",
    pinyin: "yidaliyu",
    sort: 12
  },
  vie: {
    code: 'vi-VN',
    zhName: "越南语",
    enName: "Vietnamese",
    pinyin: "yuenanyu",
    sort: 13
  },
  yue: {
    code: 'zh-HK',
    zhName: "中文(粤语)",
    enName: "Cantonese",
    pinyin: "zhongwenyueyu",
    sort: 14
  },
  cht: {
    code: 'zh-TW',
    zhName: "中文(繁体)",
    enName: "Traditional Chinese",
    pinyin: "zhongwenfanti",
    sort: 15
  }
}

// 获取按排序获取语言列表
export const getLanguagesBySort = () => {
  return Object.entries(languages)
    .sort((a, b) => a[1].sort - b[1].sort)
    .map(([key, lang]) => ({
      key,
      ...lang
    }))
}

// 根据拼音搜索语言
export const searchLanguageByPinyin = (keyword: string) => {
  const lowerKeyword = keyword.toLowerCase()
  return Object.entries(languages)
    .filter(([_, lang]) => lang.pinyin.includes(lowerKeyword))
    .map(([key, lang]) => ({
      key,
      ...lang
    }))
}

// 根据中文名搜索语言
export const searchLanguageByZhName = (keyword: string) => {
  return Object.entries(languages)
    .filter(([_, lang]) => lang.zhName.includes(keyword))
    .map(([key, lang]) => ({
      key,
      ...lang
    }))
}

// 根据英文名搜索语言
export const searchLanguageByEnName = (keyword: string) => {
  const lowerKeyword = keyword.toLowerCase()
  return Object.entries(languages)
    .filter(([_, lang]) => lang.enName.toLowerCase().includes(lowerKeyword))
    .map(([key, lang]) => ({
      key,
      ...lang
    }))
}

// 获取语言代码对应的语言信息
export const getLanguageByCode = (code: string): LanguageInfo | undefined => {
  return languages[code]
} 